package com.example.config;

import com.example.intercept.AuthInterceptor;
import com.example.resolver.AccountHandlerMethodResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private AuthInterceptor authInterceptor;
    @Autowired
    private AccountHandlerMethodResolver accountHandlerMethodResolver;

    //    解决全局的跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping(("/**"))//需要处理跨域的请求地址
                .allowedMethods("*")//允许的请求方式
                .allowedOriginPatterns("*")//允许发起请求客户端
                .allowedHeaders("*")//允许携带的请求头
                .allowCredentials(true)//是否允许携带凭证
                .maxAge(36000);
    }

    //    开启使用自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor).addPathPatterns("/**");
    }

    //    配置方法参数解析器
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(accountHandlerMethodResolver);
    }

}
